<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>操作人员管理</title>
<link rel="styleSheet" href="../../css/bootstrap.min.css"> 
<script type="text/javascript" src="jquery/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="js/pageGroup.js"></script>
<style type="text/css">
	#buttonDiv{
		margin-left:0px;
		margin-top:5px;
	}
	#tablediv{
	width: 900px;
	}
	#kong{
	width: 100px;
	}
	#kong1{
	width: 100px;
	}
	#p{
	margin-left: 100px;
	}
	#div{
		margin: 0px auto;
		margin-left:50px;
	}
	ul{ list-style:none;}
	body{
		height:600px;
	}
	/*分页*/
	#pageGro{ width:400px; height:25px; margin:0px auto; padding-top:30px;}
	#pageGro div,#pageGro div ul li{ font-size:12px; color:#999; line-height:23px; float:left; margin-left:5px;}
	#pageGro div ul li{ width:22px; text-align:center; border:1px solid #999; cursor:pointer;}
	#pageGro div ul li.on{ color:#fff; background:#3c90d9; border:1px solid #3c90d9;}
	#pageGro .pageUp,#pageGro .pageDown{ width:63px; text-indent:5px; border:1px solid #999; cursor:pointer;}
	#toolDiv{
		position: absolute;
		left:250px;
		top:300px;
	}
	td{
		text-align: center;
	}
</style>
</head>
<body>

	<div id="div">
		&nbsp;&nbsp;
		<div id="buttonDiv">
			<a href="addoperator.jsp" id="addOperator" class="btn btn-success" >新增操作员</a>
		</div>
		<br>
		<div id="tablediv">
		<input type="hidden" id="id" value="0">
			<table id="mainTable" class="table table-striped table-bordered table-hover">
				<tr>
					<td style="width:140px;">工号</td>
					<td style="width:140px;">姓名</td>
					<td style="width:340px;">权限</td>
					<td>操作</td>
				</tr>
			
			
			</table>
		</div>
		<div style="margin-top:120px;" id="toolDiv">
			<div id="pageGro">
		        <div class="pageUp"  class="btn btn-info" style="position:absolute;left:-20px;top:42px;"> 上一页</div>
		        <div class="pageList" >
		            <ul>
		                
		            </ul>
		        </div>
		        <div class="pageDown"  class="btn btn-info" style="position:absolute;left:180px;top:42px;">下一页 </div>
		    </div>
		
		<br>
		<br>
		
	</div>
</div>
<script type="text/javascript">
	var pageCount;
	$(function(){
		$.ajax({
			url:"findLevel.do",
			type:"post",
			data:{id:getParam("id")},
			async:false,
			success:function(msg){	
				if(msg.length!=0){
					$("#addOperator").remove();
				}
			}			
	   })
		init();
	})
	function init(){
		changePage(1);
		icon_load(pageCount);
	}
	function findle(){
		var flag;
		$.ajax({
			url:"findLevel.do",
			type:"post",
			data:{id:getParam("id")},
			async:false,
			success:function(msg){
				if(msg.length!=0){
					flag=true;
				}else{
					flag=false;
				}
			}			
	   })
	   return flag;
	}
	function changePage(curPage){
		
		$.ajax({
			url:"findAllOperator.do",
			type:"post",
			async:false,
			data:{"curPage":curPage},
			success:function(msg){
				$("#mainTable tr:gt(0)").remove();
				var option ="";
				$.each(msg.list,function(i,n){
					var level="";
					$.each(n.levels,function(i,n){
						level+=n.levelDescribe+" ";
					})
					option += "<tr><td>"+n.user.userName+"</td><td>"+n.operatorName+"</td><td>"+level+"</td><td><input type='button' id='"+n.user.userID+"' value='修改' class='update btn btn-warning'/><input style='margin-left:20px;' type='button' id='"+n.user.userID+"' class='delete btn btn-danger' disabled='disabled' value='删除'></td><tr>";
				})
				$("#mainTable").append(option);
				pageCount=msg.countPage;
				
			}			
		})
		return pageCount;
	}
	$(document).on("click","#pageGro li",function(){
		var pageNum = parseInt($(this).html());//获取当前页数
		var selector = $(this);
		//成功后生成分页按钮
		num_click(changePage(pageNum),pageNum,selector);
	});
	
	//点击上一页触发
	$(document).on("click","#pageGro .pageUp",function(){
		var pageNum = parseInt($("#pageGro li.on").html());//获取当前页
		var index = $("#pageGro ul li.on").index();//获取index
		var selector = $(this);
		//成功后生成分页按钮
		pageUp_click(changePage(pageNum-1),pageNum,selector);
	});
	
	//点击下一页触发
	$(document).on("click","#pageGro .pageDown",function(){
		var pageNum = parseInt($("#pageGro li.on").html());//获取当前页

		var index = $("#pageGro ul li.on").index();//获取index
		var selector = $(this);
		//成功后生成分页按钮
		pageDown_click(changePage(pageNum+1),pageNum,selector);	
	});

	$("#mainTable tr").live("click",function(){
		var id;
		if($(this).attr("id")==undefined){
			id = 0;
		}else{
			id = $(this).attr("id");
		}
		$("#id").val(id);
	})
	$(".update").live("click",function(){
		var flag=findle();
		if(flag){
			alert("你没有权限执行这项操作！")
		}else{
			var id = $(this).attr("id");
			var myId=getParam("id");
			if(findLevelss(id)==0&&myId!=id){
				alert("超管不能操作其他超管");
			}else{
				if(id==0){
					alert("请选择行！")
				}else{
					window.location.href="updateoperator.jsp?id="+id;
				}
			}
		}
	})
	$(".delete").live("click",function(){
			var id = $(this).attr("id");
			var myId=getParam("id");
			if(findLevelss(id)==0){
				alert("超管不能删除超管");
			}else{
				if(id==0){
					alert("请选择行！")
				}else{
					if(confirm("确定删除？")){
						$.ajax({
							url:"delOperatorById.do",
							type:"post",
							async:false,
							data:{"id":id},
							success:function(msg){	
								icon_load(changePage(1));
							}	
						})
					}
				}
			}
		})
		function getParam(url){
			 var reg
			 = new RegExp("(^|&)"+
			 url +"=([^&]*)(&|$)");
			 var r
			 = window.location.search.substr(1).match(reg);
			if (r!=null) return unescape(r[2]); return null;
		 }
		function findLevelss(id){
			var length=0;
			    $.ajax({
					url:"findLevel.do",
					type:"post",
					data:{id:id},
					async:false,
					success:function(msg){
						length=msg.length; 
					}			
				}) 
			return length;
		}
		function findOperatorbyUserId(id){
			var id;
			$.ajax({
				url:"findOperatorbyUserId.do",
				type:"post",
				async:false,
				data:{"id":id},
				success:function(msg){
					id = msg.user.userID;
				}			
			})
			return id;
		}
</script>
</body>
</html>